<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style type="text/css">
        @media screen and (max-width: 768px) {
            .layui-table-tool{
                line-height: 22px;
                padding: 0.5rem 0.5rem;
            }
            .layui-form-label {
                font-size: 0.7rem!important;
                padding: 0!important;
                width: 40%!important;
            }
            .layui-input-inline .layui-input{
                height: 20px!important;
                font-size: 0.6rem!important;
                padding-left: 0!important;
            }
            .layui-input-inline{
                width: 6rem!important;
            }
            .layui-table-tool-temp .layui-btn{
                height: 1.5rem;
                line-height: 1rem;
                padding: 0 0.3rem;
                font-size: 0.6rem;
            }
            .layui-table-tool-temp{
                padding-right: 0!important;
                width: 6rem!important;
            }
            .layui-table-tool-self{
                right: 0.4rem!important;
            }
            .layui-table-tool .layui-inline[lay-event] {
                width: 1rem;
                height: 1rem;
                padding: 0;
            }
            .layui-icon {
                font-size: 0.9rem;
            }
            body{
                height: 30rem!important;
            }
            .layui-table-view{
                height:100%!important;
            }
            .layui-table-body{
                height:100%!important;
            }
            .layui-table-tool{
                padding: 0.6rem 0.4rem;
            }
            .layui-table-view .layui-table{
                width: 100%!important;
            }
            .layui-table-page{
                overflow: scroll;
            }
            .layui-table-column{
                padding: 0.5rem 0.2rem;
            }
            .layui-table-page .layui-laypage a{
                padding: 0 0.4rem;
                font-size: 0.6rem;
            }
            .layui-table-page .layui-laypage span{
                font-size: 0.6rem;
            }
            .layui-table-page .layui-laypage input{
                width: 1.8rem!important;
                padding: 0.1rem;
            }
            .layui-table-cell {
                padding: 2px 4px 2px 6px;
                font-size: 0.6rem;
            }
            .laytable-cell-1-0-0{
                width: 3rem!important;
                padding: 6px 0;
                text-align: center;
            }
            .laytable-cell-1-0-1 {
                width: 70px !important;
            }
            .laytable-cell-1-0-2{
                width: 90px!important;
            }
            .layui-btn-xs {
                padding: 0 4px;
                font-size: 0.6rem;
            }
            .layui-table-fixed {
                width: 23%;
                min-width: 3rem;
            }
            /*    建立部门*/
            #builddiv{
                width: 100%!important;
                height: 200px!important;
            }
            #builddiv .layui-form-item{
                padding-top: 10px;
                font-size:0.75rem!important;
            }
            #builddiv .layui-form-item .layui-form-label {
                width: 27%!important;
                height: 25px;
                line-height: 10px;
                text-align: left;
                padding: 8px 1px 8px 3px !important;
                min-width: 60px;
            }
            #builddiv .layui-form-item .layui-input-block input{
                height: 25px;
                line-height: 10px;
            }
            #builddiv .layui-form-item .layui-input-block {
                margin-left: 60px;
                width: 60%;
            }
            #builddiv .layui-form-item .layui-input-block button{
                font-size: 0.7rem !important;
                height: 25px;
                line-height: 25px;
                padding: 0 4px;
            }
        /*    删除*/
            .layui-layer-dialog {
                width: 90% !important;
                height: 50%;
                min-width: 130px !important;
                min-height: 150px !important;
            }
            .layui-layer-dialog .layui-layer-content {
                font-size: 0.7rem !important;
            }
            .layui-layer-btn {
                padding: 0 0 12px !important;
            }
            .layui-layer-btn .layui-layer-btn0,.layui-layer-btn .layui-layer-btn1{
                font-size: 0.6rem !important;
                height: 20px;
                line-height: 20px;
                padding: 3px 2px;
                margin: 0;
            }
            .layui-layer-btn .layui-layer-btn1{
                margin: 0 10px;
            }

        }
    </style>
</head>

<body>
<script src="./layui/layui.js"></script>
<div style="display: none;width: 1200px;height: 800px" id="builddiv" >
    <form class="layui-form layui-form-pane" action="/department/add" target="/departmentShow" id="departmentForm">
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block">
                <input type="text" name="departmentName" lay-verify="required"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit=""lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<table class="layui-hide" id="departmentTable" lay-filter="departmentTable"></table>
<script type="text/html" id="toolbarDemo" >
    <div class="layui-inline">
        <label class="layui-form-label">部门名</label>
        <div class="layui-input-inline">
            <input type="text" name="departmentInput" id="departmentInput" autocomplete="off" class="layui-input">
        </div>
    </div>
    <button class="layui-btn" lay-event="query">查找</button>
    <button class="layui-btn" lay-event="build">建立部门</button>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="updateUser" style="background: #67b168">修改</a>-->
</script>
<script th:inline=none>
    layui.use(['table', 'dropdown','form'], function () {
        var table = layui.table;
        var $ = layui.$;
        var form=layui.form
        layui.form.verify({
            //表单验证
            username: function (value, item) {
                if (!/^[a-zA-Z0-9]+$/.test(value)){
                    return '请输入数字字母和下划线';
                }
                if (value.length>=30){
                    return '请输入小于三十个字符';
                }
            },
            realName: function (value, item) {
                if (value.length>=30){
                    return '请输入小于三十个字符';
                }

                if (!/[\u4e00-\u9fa5]/.test(value)) {
                    return '请输入真实姓名';
                }
            },
            password: function (value, item) {
                var regex = /^[0-9a-zA-Z_]+$/;
                if (value.length>=24){
                    return '请输入小于24个字符';
                }
                if (!regex.test(value)) {
                    return '请输入数字字母下划线';
                }
            },
            employeeNumber: function (value, item) {
                var regex = /^[0-9a-zA-Z_]+$/;
                if (value.length>=24){
                    return '请输入小于24个字符';
                }
                if (!regex.test(value)) {
                    return '请输入数字字母';
                }
            },

        })
        layui.form.on('submit(demo1)', function(data){
            //demo1表单提交（开户）
            $.post("/department/add",{
                "departmentName":data.field.departmentName,
            }, function(data) {
                if(data.code==20000){
                    layer.msg(data.msg)
                    table.reload('table1');
                }
                else {
                    $('input[name="departmentName"]').val('');
                    layer.closeAll()
                    layer.msg("新建成功")
                    table.reload('table1');
                    form.render();
                }

            });
            return false;
        });
        table.render({
            //表格渲染
            elem: '#departmentTable',
            id: 'table1',
            url: '/department/query'
            , response: {
                statusCode: 10000 //规定成功的状态码，默认：0
            }
            , toolbar: '#toolbarDemo'
            , height: 'full-200'
            , cellMinWidth: 80,
              limit:20,
              limits:[20]
            , page: true
            , cols: [[
                {field: 'departmentId', fixed: 'left', width: 80, title: '部门Id'}
                , {field: 'departmentName', title: '部门'}
                , {title: '操作', toolbar: '#barDemo'}
            ]]
            , error: function (res, msg) {
                console.log(res, msg)
            }
            , parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },

        });
        table.on('toolbar(departmentTable)', function (obj) {
            switch (obj.event) {
                case 'build':
                    layer.open({
                        title: '开号',
                        type: 1,
                        shadeClose: true,
                        content: layui.$('#builddiv')
                    });
                    break;
                case 'query':
                    var querycontent=$('#departmentInput').val()
                    table.reload('table1', {
                        where: {
                            departmentName: $('#departmentInput').val(),
                        }
                    });
                    $('#departmentInput').val(querycontent)
            }

        });
        table.on('tool(departmentTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('删除选项', {
                    btn: ['确认删除', '还是算了'] //按钮
                }, function () {
                    $.ajax({
                            url: '/department/delete?departmentId=' + data.departmentId,
                            dataType: 'json',
                            type: 'post',
                            success: function (data) {
                                layer.closeAll();
                                table.reload('table1');
                            }, error: function () {
                                alert("删除失败")
                                layer.closeAll();
                            }
                        }
                    )
                }, function () {
                    layer.closeAll();
                });

            }
            // else if (obj.event === 'updateUser'){
            //     //将点击行的数据显示到修改表单
            //     form.val("formUpdate",{
            //         "departmentId":obj.data.departmentId,
            //         "departmentName":obj.data.departmentName,
            //     });
            //     layui.use(['form'],function (){
            //         var form = layui.form;
            //         var departmentId = obj.data.departmentId;
            //         var positionId = obj.data.positionId;
            //         $("#department1").val(departmentId);
            //         $("#position1").val(positionId);
            //         form.render();
            //     });
            //     //弹出修改页面
            //     layer.open({
            //         title: '部门名称修改',
            //         type: 1,
            //         shadeClose: true,
            //         content: layui.$('#buildUpdateDiv')
            //     });
            // }
        })


    })
</script>

</body>
</html>